package cn.shadow.gateway.predicates;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;


/**
 * 所有的谓词工厂都需要以RoutePredicatesFactory为结尾
 * @author 冯啸迪
 *
 */
@Component
public class TimeBetweenRoutePredicatesFactory extends AbstractRoutePredicateFactory<TimeBetweenConfig> {
    public TimeBetweenRoutePredicatesFactory(Class configClass) {
        super(configClass);
    }

    @Override
    public Predicate<ServerWebExchange> apply(TimeBetweenConfig timeBetweenConfig) {
        LocalTime start = timeBetweenConfig.getStart();
        LocalTime end = timeBetweenConfig.getEnd();
        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                LocalTime now = LocalTime.now();
                return now.isAfter(start)&&now.isBefore(end);
            }
        };
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("start","end");
    }
}
